﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RedeSocial.ViewModels;
using RedeSocial.Validation;
using RedeSocial.DataAccess;
using RedeSocial.ViewModels.Enums;
using RedeSocial.Errors;
using System.Web.Security;
namespace RedeSocial.Web
{
	public partial class NotAuth : BasePage
	{
		#region overrides...
		protected override void CreateViewModel()
		{
			CreateUserViewModel = new CreateUserViewModel()
			{
				DefaultLanguage = Language,
				FluentValidator = new CreateUserValidator((Session["DefaultLanguage"] as Language).Id)
			};
			if (!IsPostBack)
			{
				using (var dataContext = new RedeSocialEntities())
				{
					cboCountries.DataTextField = "Name";
					cboCountries.DataValueField = "Id";
					cboCountries.DataSource = dataContext.CountryNames.Where(c => c.Language.Id == CreateUserViewModel.DefaultLanguage.Id).OrderBy(c => c.Name).Select(c => new { Id = c.Country.Id, Name = c.Name, Country = c.Country }).ToList();
					cboCountries.DataBind();
				}
			}
		}
		protected override void BindViewModel()
		{
			CreateUserViewModel.AgreeWithTOS = chkAgreeWithTOS.Checked;
			CreateUserViewModel.Birthday = rdpBirthday.SelectedDate;
			CreateUserViewModel.ConfirmPassword = txtConfPass.Text.Trim();
			CreateUserViewModel.Email = txtEMail.Text.Trim();
			CreateUserViewModel.Gender = String.Compare(cboGender.SelectedValue, "1", false) == 0 ? Gender.Male : String.Compare(cboGender.SelectedValue, "2", false) == 0 ? Gender.Female : Gender.Unknown;
			CreateUserViewModel.Name = txtName.Text.Trim();
			CreateUserViewModel.Password = txtPass.Text.Trim();
			if (IsPostBack && cboCountries.SelectedItem != null && (CreateUserViewModel.Country == null || CreateUserViewModel.Country.Id != Convert.ToInt32(cboCountries.SelectedValue)))
			{
				using (var dataContext = new RedeSocialEntities())
				{
					int id = Convert.ToInt32(cboCountries.SelectedValue);
					CreateUserViewModel.Country = dataContext.Countries.Where(c => c.Id == id).FirstOrDefault();
				}
			}

		}
		protected override void BindLabels()
		{
			CreateUserViewModel.DefaultLanguage = Session["DefaultLanguage"] as Language;
			//lblError.Text = ResourceManager.GetResource(Constants.LOGINVIEW_INVALIDUSERMESSAGE, LoginViewModel.DefaultLanguage.Id);
			lblName.Text = ResourceManager.GetResource(Constants.CREATEUSERVIEW_NAMELABEL, CreateUserViewModel.DefaultLanguage.Id);
			lblEMail.Text = ResourceManager.GetResource(Constants.CREATEUSERVIEW_EMAILLABEL, CreateUserViewModel.DefaultLanguage.Id);
			lblGender.Text = ResourceManager.GetResource(Constants.CREATEUSERVIEW_GENDERLABEL, CreateUserViewModel.DefaultLanguage.Id);
			lblbBirthday.Text = ResourceManager.GetResource(Constants.CREATEUSERVIEW_BIRTHDAYLABEL, CreateUserViewModel.DefaultLanguage.Id);
			lblPass.Text = ResourceManager.GetResource(Constants.CREATEUSERVIEW_PASSWORDLABEL, CreateUserViewModel.DefaultLanguage.Id);
			lblConfPass.Text = ResourceManager.GetResource(Constants.CREATEUSERVIEW_CONFIRMPASSWORDLABEL, CreateUserViewModel.DefaultLanguage.Id);
			lblCountries.Text = ResourceManager.GetResource(Constants.CREATEUSERVIEW_COUNTRYLABEL, CreateUserViewModel.DefaultLanguage.Id);
			RadCaptcha.ErrorMessage = ResourceManager.GetResource(Constants.CREATEUSERVIEW_CAPTCHAERRORMESSAGE, CreateUserViewModel.DefaultLanguage.Id);
			RadCaptcha.CaptchaLinkButtonText = ResourceManager.GetResource(Constants.CREATEUSERVIEW_CAPTCHALINKBUTTONTEXT, CreateUserViewModel.DefaultLanguage.Id);
			RadCaptcha.InvisibleTextBoxLabel = ResourceManager.GetResource(Constants.CREATEUSERVIEW_CAPTCHATEXTBOXLABEL, CreateUserViewModel.DefaultLanguage.Id);
			RadCaptcha.CaptchaAudioLinkButtonText = ResourceManager.GetResource(Constants.CREATEUSERVIEW_CAPTCHAAUDIOLINKBUTTONTEXT, CreateUserViewModel.DefaultLanguage.Id);
			chkAgreeWithTOS.Text = ResourceManager.GetResource(Constants.CREATEUSERVIEW_AGREEWITHTERMSCHECKBOXTEXT, CreateUserViewModel.DefaultLanguage.Id);
			lnkReadTOS.Text = ResourceManager.GetResource(Constants.CREATEUSERVIEW_READLINKTEXT, CreateUserViewModel.DefaultLanguage.Id);
			btnSend.Text = ResourceManager.GetResource(Constants.CREATEUSERVIEW_CREATEUSERBUTTONTEXT, CreateUserViewModel.DefaultLanguage.Id);
			cboItemMen.Text = ResourceManager.GetResource(Constants.CREATEUSERVIEW_MALELABEL, CreateUserViewModel.DefaultLanguage.Id);
			cboItemWoman.Text = ResourceManager.GetResource(Constants.CREATEUSERVIEW_FEMALELABEL, CreateUserViewModel.DefaultLanguage.Id);

			ValidatorAgreeWithTOS.ErrorMessage = ResourceManager.GetResource(Constants.CREATEUSERVIEW_INVALIDAGREEWITHTERMSVALIDATION, CreateUserViewModel.DefaultLanguage.Id);
			ValidatorBirthday.ErrorMessage = ResourceManager.GetResource(Constants.CREATEUSERVIEW_INVALIDBIRTHDAYVALIDATION, CreateUserViewModel.DefaultLanguage.Id);
			ValidatorConfirmPassword.ErrorMessage = ResourceManager.GetResource(Constants.CREATEUSERVIEW_INVALIDCONFIRMPASSWORDVALIDATION, CreateUserViewModel.DefaultLanguage.Id);
			ValidatorCountry.ErrorMessage = ResourceManager.GetResource(Constants.CREATEUSERVIEW_INVALIDCOUNTRYVALIDATION, CreateUserViewModel.DefaultLanguage.Id);
			ValidatorEmail.ErrorMessage = ResourceManager.GetResource(Constants.CREATEUSERVIEW_INVALIDEMAILVALIDATION, CreateUserViewModel.DefaultLanguage.Id);
			ValidatorGender.ErrorMessage = ResourceManager.GetResource(Constants.CREATEUSERVIEW_INVALIDGENDERVALIDATION, CreateUserViewModel.DefaultLanguage.Id);
			ValidatorName.ErrorMessage = ResourceManager.GetResource(Constants.CREATEUSERVIEW_INVALIDNAMEVALIDATION, CreateUserViewModel.DefaultLanguage.Id);
			ValidatorPassword.ErrorMessage = ResourceManager.GetResource(Constants.CREATEUSERVIEW_INVALIDPASSWORDVALIDATION, CreateUserViewModel.DefaultLanguage.Id);
		}
		#endregion
		#region properties...
		protected CreateUserViewModel CreateUserViewModel
		{
			get
			{
				return ViewModel as CreateUserViewModel;
			}
			set
			{
				ViewModel = value;
			}
		}
		public string TOSUrl { get { return string.Format("~/TOS/TOS{0}.pdf", Language.Code); } }
		#endregion
		#region event handles...
		protected void Page_Load(object sender, EventArgs e)
		{
			Response.Cache.SetCacheability(HttpCacheability.NoCache);
			lblError.Visible = false;
			if (!IsPostBack)
			{
				rdpBirthday.MinDate = DateTime.Now.Date.AddYears(-150);
				rdpBirthday.MaxDate = DateTime.Now.Date.AddYears(-10);
			}
		}
		#endregion

		protected void btnSend_Click(object sender, EventArgs e)
		{
			Session session;
			if (CreateUserViewModel.IsValid())
				try
				{
					session = CreateUserViewModel.CreateUser();
					if (session != null)
					{
						Session.Add("Session", session);
						FormsAuthentication.RedirectFromLoginPage(session.User.Email, false);
					}
				}
				catch (UserAlreadyExistException ex)
				{

					lblError.Text = ResourceManager.GetResource(Constants.CREATEUSERVIEW_USERALREADYEXIST, CreateUserViewModel.DefaultLanguage.Id);
					lblError.Visible = true;
				}
		}
	}
}